//-------------------------------------  获取数据  -----------------------------------------------------*/
var ip, date, tab, weSocketLink;
var chartData = [];
//下发
function wesoket(taskId){
    function getJson(taskId){
        $.ajax({
            type: 'POST',
            url: CONTEXT_PATH+'/rest/probeClientApi/getProvincePingQuery',
            data: {task_id: taskId},
            success: function(data) {
                chinaMap.hideLoading();
                $(".mark_modl").hide();
                for (var i in data) {
                    chartData.push(data[i][0]);
                }
                chinaMap.setOption({
                    series: [
                        {
                            name: 'china',
                            data: convertData(chartData)
                        }
                    ]
                });
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                console.log(textStatus + errorThrown)
            }
        });
    }
    var websocket = null;
    //判断当前浏览器是否支持WebSocket
    var url = CONTEXT_WEBSOCKET_URL+'client'+ taskId;
    if ('WebSocket' in window) {
        websocket = new WebSocket(url);
    }
    else {
        alert('当前浏览器 Not support websocket');
    }
    websocket.onmessage = function (event) {
        getJson(taskId);
    }
    websocket.onclose = function (){
        console.log('链接已关闭' + taskId);
    }

}

$('.start').on('click', function(e) {
    chinaMap.showLoading();
    ip = $('.ipName').val();
    $(".mark_modl").show();
    $.ajax({
        type: 'POST',
        url: CONTEXT_PATH+'/rest/probeTask/probeAdd',
        data: {destaddr: ip, tasktypeid: '1'},
        success: function(data) {
            if (weSocketLink) {
                weSocketLink.close();
            }
            weSocketLink = wesoket(data.taskid);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            console.log(textStatus + errorThrown)
        }
    });
});
//---------------------------------  处理数据函数  ------------------------------------------------------

//处理省数据
var convertData = function(data) {
    var res = [];
    for (var i = 0; i < data.length; i++) {
        console.log(data);
        var geoCoord = data[i].value;
        if (geoCoord === 0) {
            res.push({
                name: data[i].name,
                tooltip: {
                    show: true,
                    trigger: 'item',
                    formatter: function(params) {
                        return ( params.name )
                    }
                },
                itemStyle: {
                    normal: { areaColor: '#5FE79A' },
                    emphasis: { areaColor: '#5FE79A' }
                }
            })
        } else if (geoCoord === 1) {
            res.push({
                name: data[i].name,
                tooltip: {
                    show: true,
                    trigger: 'item',
                    formatter: function(params) {
                        return ( params.name )
                    }
                },
                itemStyle: {
                    normal: { areaColor: '#FF5D60' },
                    emphasis: { areaColor: '#FF5D60' }
                }
            })
        } else if (geoCoord === 2) {
            res.push({
                name: data[i].name,
                tooltip: {
                    show: true,
                    trigger: 'item',
                    formatter: function(params) {
                        return ( params.name )
                    }
                },
                itemStyle: {
                    normal: { areaColor: '#FCE353' },
                    emphasis: { areaColor: '#FCE353' }
                }
            })
        }
    }
    return res;
};

//返回全国地图
var chinaMap = echarts.init(document.getElementById('map'));
//--------------------------------- 全国 -------------------------------------------------------------
//获取地图
$.ajax({
    type: 'GET',
    url: 'json/china.json',
    async: false,
    success: function(chinaJson) {
        echarts.registerMap('china', chinaJson);
        chinaMap.hideLoading();
        ChinaMaps();
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        console.log(textStatus + errorThrown)
    }
});

//显示全国地图
function ChinaMaps() {
    chinaMap.setOption({
        
        backgroundColor: '#F4F3F4',
        title: {
            text: '中国地图',
            top: 30,
            left: 'center',
            textStyle: { color: 'rgb(22, 24, 35)' }
        },
        tooltip: {
            show: false,
            trigger: 'item'
        },
        geo: {
            map: 'china',
            zoom: 1.2,
            itemStyle: {
                normal: {
                    shadowBlur: 50,
                    shadowColor: 'rgba(0, 0, 0, 0.8)'
                }
            }
        },
        series: [{
            name: 'china',
            type: 'map',
            map: 'china',
            zoom: 1.2,
            label: {
                normal: { show: false },
                emphasis: { show: false }
            },
            itemStyle: {
                normal: {
                    areaColor: '#F4F3F4',
                    borderColor: '#ccc',
                    shadowBlur: 50,
                    shadowColor: 'rgba(0, 0, 0, 0.3)'
                },
                emphasis: { areaColor: '#F4F3F4', borderColor: '#ccc' }
            },
            data: []
        }]
    });
};